<?php
namespace app\api\controller;

use app\admin\model\hcbaorecord\Hcbaorecord;
use app\common\controller\Api;
use think\Db;
use think\Exception;

class Hcbao extends Api{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function buy(){
        $userinfo = $this->auth->getUserinfo();
        $amount = $this->request->param('amount');
        $hcbaoModel = new \app\admin\model\hcbao\Hcbao();
        $info = $hcbaoModel->where(['user_id'=>$userinfo['id']])->find();
        $systemModel = new \app\admin\model\system\System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();
        if(!$amount || $amount==0){
            $this->error('转入金额必须大于0');
        }
        if($amount>$userinfo['shouyi_balance']){
            $this->error('余额不足');
        }
        if($userinfo['is_freeze']==1){
            $this->error('您的帐户被冻结，不能购买HC宝');
        }
        Db::startTrans();
        try{
            if($info){
                $info = $info->toArray();
                //修改用户余额
                $userModel = new \app\admin\model\User();
                $userModel->where(['id'=>$userinfo['id']])->update([
                    'shouyi_balance'=>$userinfo['shouyi_balance']-$amount,
                ]);
                //修改HC宝
                $hcbaoModel->where(['user_id'=>$userinfo['id']])->update([
                    'amount'=>$info['amount']+$amount,
                ]);
                $new_amount=$info['amount']+$amount;
                $hcbaoModel->where(['user_id'=>$userinfo['id']])->update([
                    'yugu_amount'=>$new_amount*$systeminfo['hc_bao']/100,
                ]);
            }else{
                //修改用户余额
                $userModel = new \app\admin\model\User();
                $userModel->where(['id'=>$userinfo['id']])->update([
                    'shouyi_balance'=>$userinfo['shouyi_balance']-$amount,
                ]);
                //新增HC宝
                $indata['user_id'] = $userinfo['id'];
                $indata['amount'] = $amount;
                $indata['yugu_amount'] = $amount*$systeminfo['hc_bao']/100;
                $indata['create_time'] = time();
                $hcbaoModel->insert($indata);
            }
            Db::commit();
            $this->success('转入成功');
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    public function sell(){
        $userinfo = $this->auth->getUserinfo();
        if($userinfo['is_freeze']==1){
            $this->error('您的帐户被冻结，不能转出HC宝');
        }
        $amount = $this->request->param('amount');
        $userModel = new \app\admin\model\User();
        $hcbaoModel = new \app\admin\model\hcbao\Hcbao();
        if(!$amount || $amount==0){
            $this->error('转出金额必须大于0');
        }
        $info = $hcbaoModel->where(['user_id'=>$userinfo['id']])->find();
        if($info){
            $info = $info->toArray();
            $all_amount = $info['amount']+$info['send_amount'];
            if($amount>$all_amount){
                $this->error('您的取出金额超过了您的HC宝余额');
            }
            Db::startTrans();
            try{
                if($amount > $info['amount']){
                    //先修改HC宝
                    $hcbaoModel->where(['user_id'=>$userinfo['id']])->update([
                        'amount'=>0,
                    ]);
                    $sheng=$amount-$info['amount'];
                    $hcbaoModel->where(['user_id'=>$userinfo['id']])->update([
                        'amount'=>$info['send_amount']-$sheng,
                    ]);
                    //修改用户余额
                    $userModel->where(['id'=>$userinfo['id']])->update([
                        'shouyi_balance'=>$userinfo['shouyi_balance']+$amount,
                    ]);
                }else{
                    //先修改HC宝
                    $hcbaoModel->where(['user_id'=>$userinfo['id']])->update([
                        'amount'=>$info['amount']-$amount,
                    ]);
                    //修改用户余额
                    $userModel->where(['id'=>$userinfo['id']])->update([
                        'shouyi_balance'=>$userinfo['shouyi_balance']+$amount,
                    ]);
                }
                Db::commit();
                $this->success('取出成功');
            }catch (Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }else{
            $this->error('您还未投资HC宝，不能取出');
        }
    }

    public function info(){
        $userinfo = $this->auth->getUserinfo();
        $hcbaoModel = new \app\admin\model\hcbao\Hcbao();
        $info = $hcbaoModel->where(['user_id'=>$userinfo['id']])->find();
        if($info){
            $info = $info->toArray();
            $backData=[
                'all_amount'=>$info['amount']+$info['send_amount'],
                'yesterday_amount'=>$info['yesterday_amount'],
                'leiji_amount'=>$info['send_amount'],
                'yugu_amount'=>$info['yugu_amount'],
            ];
            $this->success('成功',$backData);
        }else{
            $this->success('成功',[
                'all_amount'=>0,
                'yesterday_amount'=>0,
                'leiji_amount'=>0,
                'yugu_amount'=>0
            ]);
        }
    }

    public function list(){
        $userinfo = $this->auth->getUserinfo();
        $page = $this->request->param('page',1);
        $pageSize = 10;
        $start = ($page-1)*$pageSize;
        $hcbaorecordModel = new Hcbaorecord();

        $list = collection($hcbaorecordModel->where(['user_id'=>$userinfo['id']])->limit($start,$pageSize)->order('id desc')->select());
        if($list){
            $list =$list->toArray();
            $this->success('成功',$list);
        }else{
            $this->success('成功',[]);
        }
    }
}
